/*
 * Sudoku.java
 *
 * Created on 4 agosto 2005, 20.32
 */
package it.luigiarpino.sudoku;


import java.util.Random;
import java.util.Date;

/**
 *
 * @author  Amministratore
 */
public class Sudoku {
    
    private int Tabella[][] =new int[10][10];
    private int Schema[][]=new int[10][10];
    private int Maschera[][] =new int[10][10];
    
    
    /** Creates a new instance of Sudoku */
    public Sudoku(int difficolta,boolean isStandard) {
        if (isStandard){
            this.StandardSudoku(difficolta);
            
        }
        else{
            
            this.Inizializza();
            this.Mischia();
            this.Associa();
            this.Mischia();
            this.Associa();
            this.Mischia();
            this.Associa();
            this.Mischia();
            this.Associa();
            this.Calcolaschema(difficolta);
        }
    
    }
    
    private void StandardSudoku(int difficolta){
                Tabella[1][1]=9;
                Tabella[1][2]=6;
                Tabella[1][3]=3;
                Tabella[1][4]=1;
                Tabella[1][5]=7;
                Tabella[1][6]=4;
                Tabella[1][7]=2;
                Tabella[1][8]=5;
                Tabella[1][9]=8;

                Tabella[2][1]=1;
                Tabella[2][2]=7;
                Tabella[2][3]=8;
                Tabella[2][4]=3;
                Tabella[2][5]=2;
                Tabella[2][6]=5;
                Tabella[2][7]=6;
                Tabella[2][8]=4;
                Tabella[2][9]=9;

                Tabella[3][1]=2;
                Tabella[3][2]=5;
                Tabella[3][3]=4;
                Tabella[3][4]=6;
                Tabella[3][5]=8;
                Tabella[3][6]=9;
                Tabella[3][7]=7;
                Tabella[3][8]=3;
                Tabella[3][9]=1;

                Tabella[4][1]=8;
                Tabella[4][2]=2;
                Tabella[4][3]=1;
                Tabella[4][4]=4;
                Tabella[4][5]=3;
                Tabella[4][6]=7;
                Tabella[4][7]=5;
                Tabella[4][8]=9;
                Tabella[4][9]=6;

                Tabella[5][1]=4;
                Tabella[5][2]=9;
                Tabella[5][3]=6;
                Tabella[5][4]=8;
                Tabella[5][5]=5;
                Tabella[5][6]=2;
                Tabella[5][7]=3;
                Tabella[5][8]=1;
                Tabella[5][9]=7;

                Tabella[6][1]=7;
                Tabella[6][2]=3;
                Tabella[6][3]=5;
                Tabella[6][4]=9;
                Tabella[6][5]=6;
                Tabella[6][6]=1;
                Tabella[6][7]=8;
                Tabella[6][8]=2;
                Tabella[6][9]=4;

                Tabella[7][1]=5;
                Tabella[7][2]=8;
                Tabella[7][3]=9;
                Tabella[7][4]=7;
                Tabella[7][5]=1;
                Tabella[7][6]=3;
                Tabella[7][7]=4;
                Tabella[7][8]=6;
                Tabella[7][9]=2;

                Tabella[8][1]=3;
                Tabella[8][2]=1;
                Tabella[8][3]=7;
                Tabella[8][4]=2;
                Tabella[8][5]=4;
                Tabella[8][6]=6;
                Tabella[8][7]=9;
                Tabella[8][8]=8;
                Tabella[8][9]=5;

                Tabella[9][1]=6;
                Tabella[9][2]=4;
                Tabella[9][3]=2;
                Tabella[9][4]=5;
                Tabella[9][5]=9;
                Tabella[9][6]=8;
                Tabella[9][7]=1;
                Tabella[9][8]=7;
                Tabella[9][9]=3;
                this.Calcolaschema(difficolta);
    }
    

    private void Inizializza(){
        Random r=new Random();
        Date d=new Date();
        r.setSeed(d.getTime());
        switch (r.nextInt(4)){
            case 0:
                Tabella[1][1]=9;
                Tabella[1][2]=6;
                Tabella[1][3]=3;
                Tabella[1][4]=1;
                Tabella[1][5]=7;
                Tabella[1][6]=4;
                Tabella[1][7]=2;
                Tabella[1][8]=5;
                Tabella[1][9]=8;

                Tabella[2][1]=1;
                Tabella[2][2]=7;
                Tabella[2][3]=8;
                Tabella[2][4]=3;
                Tabella[2][5]=2;
                Tabella[2][6]=5;
                Tabella[2][7]=6;
                Tabella[2][8]=4;
                Tabella[2][9]=9;

                Tabella[3][1]=2;
                Tabella[3][2]=5;
                Tabella[3][3]=4;
                Tabella[3][4]=6;
                Tabella[3][5]=8;
                Tabella[3][6]=9;
                Tabella[3][7]=7;
                Tabella[3][8]=3;
                Tabella[3][9]=1;

                Tabella[4][1]=8;
                Tabella[4][2]=2;
                Tabella[4][3]=1;
                Tabella[4][4]=4;
                Tabella[4][5]=3;
                Tabella[4][6]=7;
                Tabella[4][7]=5;
                Tabella[4][8]=9;
                Tabella[4][9]=6;

                Tabella[5][1]=4;
                Tabella[5][2]=9;
                Tabella[5][3]=6;
                Tabella[5][4]=8;
                Tabella[5][5]=5;
                Tabella[5][6]=2;
                Tabella[5][7]=3;
                Tabella[5][8]=1;
                Tabella[5][9]=7;

                Tabella[6][1]=7;
                Tabella[6][2]=3;
                Tabella[6][3]=5;
                Tabella[6][4]=9;
                Tabella[6][5]=6;
                Tabella[6][6]=1;
                Tabella[6][7]=8;
                Tabella[6][8]=2;
                Tabella[6][9]=4;

                Tabella[7][1]=5;
                Tabella[7][2]=8;
                Tabella[7][3]=9;
                Tabella[7][4]=7;
                Tabella[7][5]=1;
                Tabella[7][6]=3;
                Tabella[7][7]=4;
                Tabella[7][8]=6;
                Tabella[7][9]=2;

                Tabella[8][1]=3;
                Tabella[8][2]=1;
                Tabella[8][3]=7;
                Tabella[8][4]=2;
                Tabella[8][5]=4;
                Tabella[8][6]=6;
                Tabella[8][7]=9;
                Tabella[8][8]=8;
                Tabella[8][9]=5;

                Tabella[9][1]=6;
                Tabella[9][2]=4;
                Tabella[9][3]=2;
                Tabella[9][4]=5;
                Tabella[9][5]=9;
                Tabella[9][6]=8;
                Tabella[9][7]=1;
                Tabella[9][8]=7;
                Tabella[9][9]=3;
                break;
            case 1:
                Tabella[1][1]=3;
                Tabella[1][2]=2;
                Tabella[1][3]=4;
                Tabella[1][4]=7;
                Tabella[1][5]=5;
                Tabella[1][6]=1;
                Tabella[1][7]=8;
                Tabella[1][8]=9;
                Tabella[1][9]=6;

                Tabella[2][1]=1;
                Tabella[2][2]=8;
                Tabella[2][3]=5;
                Tabella[2][4]=9;
                Tabella[2][5]=3;
                Tabella[2][6]=6;
                Tabella[2][7]=2;
                Tabella[2][8]=7;
                Tabella[2][9]=4;

                Tabella[3][1]=7;
                Tabella[3][2]=9;
                Tabella[3][3]=6;
                Tabella[3][4]=2;
                Tabella[3][5]=8;
                Tabella[3][6]=4;
                Tabella[3][7]=5;
                Tabella[3][8]=1;
                Tabella[3][9]=3;

                Tabella[4][1]=9;
                Tabella[4][2]=3;
                Tabella[4][3]=8;
                Tabella[4][4]=4;
                Tabella[4][5]=7;
                Tabella[4][6]=2;
                Tabella[4][7]=6;
                Tabella[4][8]=5;
                Tabella[4][9]=1;

                Tabella[5][1]=5;
                Tabella[5][2]=1;
                Tabella[5][3]=7;
                Tabella[5][4]=8;
                Tabella[5][5]=6;
                Tabella[5][6]=3;
                Tabella[5][7]=4;
                Tabella[5][8]=2;
                Tabella[5][9]=9;

                Tabella[6][1]=4;
                Tabella[6][2]=6;
                Tabella[6][3]=2;
                Tabella[6][4]=5;
                Tabella[6][5]=1;
                Tabella[6][6]=9;
                Tabella[6][7]=3;
                Tabella[6][8]=8;
                Tabella[6][9]=7;

                Tabella[7][1]=8;
                Tabella[7][2]=5;
                Tabella[7][3]=9;
                Tabella[7][4]=6;
                Tabella[7][5]=4;
                Tabella[7][6]=7;
                Tabella[7][7]=1;
                Tabella[7][8]=3;
                Tabella[7][9]=2;

                Tabella[8][1]=6;
                Tabella[8][2]=7;
                Tabella[8][3]=1;
                Tabella[8][4]=3;
                Tabella[8][5]=2;
                Tabella[8][6]=5;
                Tabella[8][7]=9;
                Tabella[8][8]=4;
                Tabella[8][9]=8;

                Tabella[9][1]=2;
                Tabella[9][2]=4;
                Tabella[9][3]=3;
                Tabella[9][4]=1;
                Tabella[9][5]=9;
                Tabella[9][6]=8;
                Tabella[9][7]=7;
                Tabella[9][8]=6;
                Tabella[9][9]=5;
                break;
            case 2:
                Tabella[1][1]=6;
                Tabella[1][2]=7;
                Tabella[1][3]=4;
                Tabella[1][4]=8;
                Tabella[1][5]=5;
                Tabella[1][6]=2;
                Tabella[1][7]=9;
                Tabella[1][8]=1;
                Tabella[1][9]=3;

                Tabella[2][1]=3;
                Tabella[2][2]=8;
                Tabella[2][3]=2;
                Tabella[2][4]=9;
                Tabella[2][5]=7;
                Tabella[2][6]=1;
                Tabella[2][7]=6;
                Tabella[2][8]=4;
                Tabella[2][9]=5;

                Tabella[3][1]=9;
                Tabella[3][2]=1;
                Tabella[3][3]=5;
                Tabella[3][4]=6;
                Tabella[3][5]=4;
                Tabella[3][6]=3;
                Tabella[3][7]=7;
                Tabella[3][8]=2;
                Tabella[3][9]=8;

                Tabella[4][1]=4;
                Tabella[4][2]=2;
                Tabella[4][3]=8;
                Tabella[4][4]=5;
                Tabella[4][5]=1;
                Tabella[4][6]=9;
                Tabella[4][7]=3;
                Tabella[4][8]=6;
                Tabella[4][9]=7;

                Tabella[5][1]=1;
                Tabella[5][2]=9;
                Tabella[5][3]=7;
                Tabella[5][4]=4;
                Tabella[5][5]=3;
                Tabella[5][6]=6;
                Tabella[5][7]=5;
                Tabella[5][8]=8;
                Tabella[5][9]=2;

                Tabella[6][1]=5;
                Tabella[6][2]=3;
                Tabella[6][3]=6;
                Tabella[6][4]=2;
                Tabella[6][5]=8;
                Tabella[6][6]=7;
                Tabella[6][7]=4;
                Tabella[6][8]=9;
                Tabella[6][9]=1;

                Tabella[7][1]=7;
                Tabella[7][2]=4;
                Tabella[7][3]=9;
                Tabella[7][4]=3;
                Tabella[7][5]=2;
                Tabella[7][6]=8;
                Tabella[7][7]=1;
                Tabella[7][8]=5;
                Tabella[7][9]=6;

                Tabella[8][1]=8;
                Tabella[8][2]=6;
                Tabella[8][3]=3;
                Tabella[8][4]=1;
                Tabella[8][5]=9;
                Tabella[8][6]=5;
                Tabella[8][7]=2;
                Tabella[8][8]=7;
                Tabella[8][9]=4;

                Tabella[9][1]=2;
                Tabella[9][2]=5;
                Tabella[9][3]=1;
                Tabella[9][4]=7;
                Tabella[9][5]=6;
                Tabella[9][6]=4;
                Tabella[9][7]=8;
                Tabella[9][8]=3;
                Tabella[9][9]=9;
                break;
            case 3:
                Tabella[1][1]=4;
                Tabella[1][2]=1;
                Tabella[1][3]=9;
                Tabella[1][4]=3;
                Tabella[1][5]=2;
                Tabella[1][6]=5;
                Tabella[1][7]=8;
                Tabella[1][8]=7;
                Tabella[1][9]=6;

                Tabella[2][1]=6;
                Tabella[2][2]=2;
                Tabella[2][3]=7;
                Tabella[2][4]=8;
                Tabella[2][5]=1;
                Tabella[2][6]=9;
                Tabella[2][7]=4;
                Tabella[2][8]=5;
                Tabella[2][9]=3;

                Tabella[3][1]=5;
                Tabella[3][2]=3;
                Tabella[3][3]=8;
                Tabella[3][4]=4;
                Tabella[3][5]=6;
                Tabella[3][6]=7;
                Tabella[3][7]=2;
                Tabella[3][8]=1;
                Tabella[3][9]=9;

                Tabella[4][1]=1;
                Tabella[4][2]=9;
                Tabella[4][3]=4;
                Tabella[4][4]=7;
                Tabella[4][5]=8;
                Tabella[4][6]=3;
                Tabella[4][7]=6;
                Tabella[4][8]=2;
                Tabella[4][9]=5;

                Tabella[5][1]=2;
                Tabella[5][2]=8;
                Tabella[5][3]=5;
                Tabella[5][4]=1;
                Tabella[5][5]=4;
                Tabella[5][6]=6;
                Tabella[5][7]=3;
                Tabella[5][8]=9;
                Tabella[5][9]=7;

                Tabella[6][1]=3;
                Tabella[6][2]=7;
                Tabella[6][3]=6;
                Tabella[6][4]=5;
                Tabella[6][5]=9;
                Tabella[6][6]=2;
                Tabella[6][7]=1;
                Tabella[6][8]=4;
                Tabella[6][9]=8;

                Tabella[7][1]=9;
                Tabella[7][2]=4;
                Tabella[7][3]=1;
                Tabella[7][4]=6;
                Tabella[7][5]=5;
                Tabella[7][6]=8;
                Tabella[7][7]=7;
                Tabella[7][8]=3;
                Tabella[7][9]=2;

                Tabella[8][1]=8;
                Tabella[8][2]=5;
                Tabella[8][3]=3;
                Tabella[8][4]=2;
                Tabella[8][5]=7;
                Tabella[8][6]=4;
                Tabella[8][7]=9;
                Tabella[8][8]=6;
                Tabella[8][9]=1;

                Tabella[9][1]=7;
                Tabella[9][2]=6;
                Tabella[9][3]=2;
                Tabella[9][4]=9;
                Tabella[9][5]=3;
                Tabella[9][6]=1;
                Tabella[9][7]=5;
                Tabella[9][8]=8;
                Tabella[9][9]=4;
                break;
           
            
        }     
    }
    
    
    private void SwapRow(int riga1,int riga2) {
        for(int i=1;i<=9;i++){
            int tmp;
            tmp=Tabella[riga1][i];
            Tabella[riga1][i]=Tabella[riga2][i];
            Tabella[riga2][i]=tmp;
        }
        
    }
    
    
    private void SwapCol(int colonna1,int colonna2) {
        for(int i=1;i<=9;i++){
            int tmp;
            tmp=Tabella[i][colonna1];
            Tabella[i][colonna1]=Tabella[i][colonna2];
            Tabella[i][colonna2]=tmp;
        }
    }
    
    
    
    private void MischiaQuadrato(int min_riga,int min_colonna) {
        Random ran=new Random();
        Date d=new Date();
        ran.setSeed(d.getTime());
        SwapRow(min_riga + ran.nextInt(3), min_riga + ran.nextInt(3));
        SwapCol(min_colonna +ran.nextInt(3),min_colonna+ran.nextInt(3));
        SwapRow(min_riga + ran.nextInt(3), min_riga + ran.nextInt(3));
        SwapCol(min_colonna +ran.nextInt(3),min_colonna+ran.nextInt(3));
        SwapRow(min_riga + ran.nextInt(3), min_riga + ran.nextInt(3));
        SwapCol(min_colonna +ran.nextInt(3),min_colonna+ran.nextInt(3));
        SwapRow(min_riga + ran.nextInt(3), min_riga + ran.nextInt(3));
        SwapCol(min_colonna +ran.nextInt(3),min_colonna+ran.nextInt(3));
    }
    
    
    private void Mischia(){
        MischiaQuadrato(1,1);
        MischiaQuadrato(1,4);
        MischiaQuadrato(1,7);
        MischiaQuadrato(4,1);
        MischiaQuadrato(4,4);
        MischiaQuadrato(4,7);
        MischiaQuadrato(7,1);
        MischiaQuadrato(7,4);
        MischiaQuadrato(7,7);
    }
    
   
    public void StampaSoluzione() {
        String txt=new String();
        for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                txt=txt+" "+Tabella[i][j];
            }
            txt=txt+"\n";
        }
        System.out.println(txt);
    }
    
    
    
    public Integer GetElemSol(int riga, int colonna) {
        Integer i=new Integer(Tabella[riga][colonna]);
        return i;
    }
    
    private void Calcolaschema(int num){
        int somma=0;
        for (int i=1;i<=9;i++){
            for (int j=1;j<=9;j++){
                somma=somma+this.Maschera[i][j];
            }
        }
        if (somma==81){
            return;
        }
        Random r=new Random();
        Date d=new Date();
        r.setSeed(d.getTime());
        for(int i=1;i<=9;i++) {
            for(int j=1; j<=9;j++) {
                int k=r.nextInt(num);
                if (k==0){
                    Maschera[i][j]=1;
                }
                else {
                    Maschera[i][j]=0;
                }
            }
        }
        for(int i=1;i<=9;i++) {
            for(int j=1; j<=9;j++) {
                Schema[i][j]=Tabella[i][j]*Maschera[i][j];
            }
        }
    }
    
    public void StampaSchema() {
        String txt=new String();
        for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                if (Schema[i][j]!=0){
                    txt=txt+" "+Schema[i][j];
                }
                else {
                    txt=txt+" "+" ";
                }
            }
            txt=txt+"\n";
        }
        System.out.println(txt);
    }
    
    public Integer GetElemSchema(int riga, int colonna) {
        Integer i=new Integer(Schema[riga][colonna]);
        return i;        
    }
    
    private void SwapNum(int num1, int num2) {
        for(int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                if (Tabella[i][j]==num1){
                    Tabella[i][j]=num2;
                }
                else if (Tabella[i][j]==num2){
                    Tabella[i][j]=num1;
                }
            }
        }
    }
    
    private void Associa() {
        Random r=new Random();
        Date d=new Date();
        r.setSeed(d.getTime());
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
        this.SwapNum(r.nextInt(9)+1,r.nextInt(9)+1);
    }
    
    public static void main(String[] args){

        
    }
    

    
    public void SuggerisciMossa() {
        int somma=0;
        for (int i=1;i<=9;i++){
            for(int j=1;j<=9;j++){
                somma=somma+this.Maschera[i][j];
            }
        }
        if (somma==81){
            return;
        }
        Random ran=new Random();
        Date d=new Date();
        ran.setSeed(d.getTime());
        for (;;){
            int riga=ran.nextInt(9)+1;
            int colonna=ran.nextInt(9)+1;
            if (this.Maschera[riga][colonna]==0){
                this.Maschera[riga][colonna]=1;
                this.Schema[riga][colonna]=this.Tabella[riga][colonna];
                break;
            }
        }
        
    }    

    
    }// Fine classe

    

